package thread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 阿里代码规范不允许使用ThreadPoolExecutor的静态方法
 * 1.创建ThreadPoolExecutor对象，设置核心线程数，最大线程数，非核心线程存活时间，队列（大小），还有拒绝策略：默认是队列和线程池都满了拒绝并报错
 * 2.使用这个executor即可，submit，最后shutdown即可，可以一次提交多个任务：executor.invokeAll()
 */
public class ThreadPoolExecutorDemo implements Callable<Integer> {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2000));
        ThreadPoolExecutorDemo task = new ThreadPoolExecutorDemo();
        for (int i=0; i<100; i++) {
            executor.submit(task);
        }
        executor.shutdown();
    }

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "在执行");
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + "执行结束");
        return new Random().nextInt();
    }
}
